package com.diploma.interaction.controller;

import com.alibaba.fastjson.JSON;
import com.diploma.interaction.model.WxxcxHdxx;
import com.diploma.interaction.model.WxxcxWz;
import com.diploma.interaction.service.WzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value = "/v1/wz")
@Slf4j
public class WzController {

    @Autowired
    WzService wzService;

    @PostMapping(value = "createArticle",produces = "application/json")
    public void createArticle(@RequestBody WxxcxWz wz){
        log.info("日志要这么写,article={}", JSON.toJSONString(wz));
        wzService.insertSelective(wz);
    }

    @PostMapping(value = "setArticle",produces = "application/json")
    public void setArticle(@RequestBody WxxcxWz wz){
        log.info("日志要这么写,article={}", JSON.toJSONString(wz));
        wzService.updateByIdSelective(wz,wz.getWzid().intValue());
    }

    @PostMapping(value = "getStaffArticleList",produces = "application/json")
    public List<WxxcxWz> getStaffArticleList(@RequestBody String staffId){
        log.info("日志要这么写,staffId={}", JSON.toJSONString(staffId));
        return wzService.selectByStaffId(staffId);
    }

    @PostMapping(value = "getHistoryArticleList",produces = "application/json")
    public List<WxxcxWz> getHistoryArticleList(@RequestBody String staffId){
        log.info("日志要这么写,staffId={}", JSON.toJSONString(staffId));
        return wzService.selectByStaffIdZt(staffId);
    }

    @PostMapping(value = "getUserArticleList",produces = "application/json")
    public List<WxxcxWz> getUserArticleList(){
        return wzService.selectByZt();
    }

    @PostMapping(value = "getArticleDetail",produces = "application/json")
    public WxxcxWz getArticleDetail(@RequestBody WxxcxWz wz){
        //阅读量+1
        WxxcxWz cur=wzService.selectById(wz.getWzid().intValue());
        short ydl;
        try {
            ydl=cur.getYdl();
        }
        catch (NullPointerException e) {
            // some getter is null
            ydl=0;
        }
        log.info("日志要这么写,ydl={}", JSON.toJSONString(ydl));
        cur.setYdl((short) (ydl+1));
        wzService.updateByIdSelective(cur,wz.getWzid().intValue());
        return cur;
    }

}
